package timing.ukulele.third.controller.msg;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import timing.ukulele.common.data.ResponseData;
import timing.ukulele.data.third.data.msg.MessageTemplateDTO;
import timing.ukulele.data.third.data.msg.SendMessageDTO;
import timing.ukulele.data.third.data.msg.TestSendMessageDTO;
import timing.ukulele.data.third.view.msg.MessageTemplateVO;
import timing.ukulele.third.service.msg.MessageTemplateService;
import timing.ukulele.third.service.msg.SendMessageService;
import java.util.Objects;

/**
 * 消息模板管理Controller
 *
 * @author zh
 */
@Slf4j
@RequestMapping("/msg/messageTemplate")
@RestController
public class MessageTemplateController {

    private final MessageTemplateService service;
    private final SendMessageService sendService;

    public MessageTemplateController(MessageTemplateService service,
                                     SendMessageService sendService) {
        this.service = service;
        this.sendService = sendService;
    }

    /**
     * /msg/messageTemplate/addMessageTemplate
     * @param dto
     * @return
     */
    @PostMapping("/addMessageTemplate")
    @Operation(summary = "新增模板")
    public ResponseData<Boolean> addMessageTemplate(@RequestBody MessageTemplateDTO dto) {
        return ResponseData.success(service.addMessageTemplate(dto));
    }

    /**
     * /msg/messageTemplate/updateMessageTemplate
     * @param dto
     * @return
     */
    @PutMapping("/updateMessageTemplate")
    @Operation(summary = "修改模板")
    public ResponseData<Boolean> updateMessageTemplate(@RequestBody MessageTemplateDTO dto) {
        return ResponseData.success(service.updateMessageTemplate(dto));
    }

    /**
     * /msg/messageTemplate/deleteMessageTemplate
     * @param id
     * @return
     */
    @DeleteMapping("/deleteMessageTemplate")
    @Operation(summary = "删除模板")
    public ResponseData<Boolean> deleteMessageTemplate(@RequestParam("id") Long id) {
        return ResponseData.success(service.deleteMessageTemplate(id));
    }

    /**
     * /msg/messageTemplate/copy
     * @param id
     * @return
     */
    @GetMapping("/copy")
    @Operation(summary = "复制模版")
    public ResponseData<Boolean> copy(@RequestParam("id") Long id) {
        return ResponseData.success(service.copy(id));
    }

    /**
     * /msg/messageTemplate/getDetails
     * @param id
     * @return
     */
    @GetMapping("/getDetails")
    @Operation(summary = "获取模板详情")
    public ResponseData<MessageTemplateVO> getDetails(@RequestParam("id") Long id) {
        return ResponseData.success(service.getDetails(id));
    }

    /**
     * /msg/messageTemplate/page
     * @param dto
     * @return
     */
    @Operation(summary = "分页查询")
    @GetMapping("/page")
    public ResponseData<IPage<MessageTemplateVO>> pageList(MessageTemplateDTO dto) {
        return ResponseData.success(service.pageList(dto));
    }

    /**
     * /msg/messageTemplate/test
     */
    @PostMapping("/testSend")
    @Operation(summary = "测试发送接口")
    public ResponseData<Boolean> testSend(@RequestBody TestSendMessageDTO dto) {
        SendMessageDTO sendMessageDTO = new SendMessageDTO();
        sendMessageDTO.setMessageTemplateId(dto.getMessageTemplateId());
        sendMessageDTO.setVariables(dto.getVariables());
        sendMessageDTO.setReceiver(dto.getReceiver());
        sendService.sendMessageByBusiness(sendMessageDTO);
        return ResponseData.success();
    }

    /**
     * /msg/messageTemplate/testRecall
     */
    @PostMapping("/recall")
    @Operation(summary = "测试撤回接口（根据模板ID撤回）")
    public ResponseData<Boolean> testRecall(@RequestParam("id") String id) {

        return ResponseData.success();
    }

}

